//
//  HXLPlaceDetailViewController.m
//  On The Load
//
//  Created by 晓琳 on 15/11/10.
//  Copyright © 2015年 @@叶一闽. All rights reserved.
//

#import "HXLPlaceDetailViewController.h"
#import "HXLTravelPlansViewController.h"
#import "HXLCityModel.h"
#import "BaseTableView.h"
#import "HXLLeavesModel.h"
#import "DataHandle.h"



@interface HXLPlaceDetailViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>

@property (nonatomic, strong) BaseTableView *detailTV;
@property (nonatomic, strong) UISearchBar *searchBar;
@property (nonatomic, assign) BOOL active;
@property (nonatomic, strong) BaseView *searchView;
@property (nonatomic, strong) NSMutableArray *searchArray;
@property (nonatomic, strong) NSMutableArray *allArray;

@end

@implementation HXLPlaceDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//     Do any additional setup after loading the view.
    
        self.searchView = [[BaseView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        [self.view addSubview:self.searchView];
        self.searchView.alpha = 0.6;
        self.searchView.backgroundColor = [UIColor blackColor];
    
    
    self.searchArray = [NSMutableArray array];
    self.allArray = [NSMutableArray array];
    
    self.detailTV = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-49) style:UITableViewStylePlain];
    self.detailTV.delegate = self;
    self.detailTV.dataSource = self;
    [self.view addSubview:self.detailTV];
    self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 44)];
    self.searchBar.delegate = self;
    [self.searchBar setPlaceholder:@"搜索你想去的城市"];
    self.active = NO;
    self.detailTV.tableHeaderView = self.searchBar;
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarAction:)];

    
}


- (void) rightBarAction:(UIBarButtonItem *)bar {
    
   [self.navigationController popViewControllerAnimated:YES]
    ;
    
}


//实现搜索功能
-(void)reloadMytableView
{
    self.active = YES;
    
    [self.allArray removeAllObjects];
    [self.searchArray removeAllObjects];
    
    
    
    NSMutableArray *array1 = [[DataHandle shareDataHandle].finDic objectForKey:@"areas"];
    NSLog(@"arrr = %ld",array1.count);
    NSArray *array2 = [[DataHandle shareDataHandle].finDic objectForKey:@"areas2"];
    
    [array1 addObjectsFromArray:array2];
    for (HXLCityModel *model in array1) {
        
        for (HXLLeavesModel *smallModel in model.leavesArray) {
            [self.allArray addObject:smallModel];
        }
    }
    
    
    [self.detailTV reloadData];
}

//搜索事件
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    

    NSLog(@"searchText = %@",searchText);
    if ([searchText isEqualToString:@""]) {
        self.active = NO;
        [self.detailTV reloadData];
        return;
    }
    [self reloadMytableView];
    
    NSLog(@"输入框中的内容=====%@",searchText);
    for (HXLLeavesModel *model in self.allArray) {
        
        //rangeOfString:方法是在movieName中搜索是否包含searchText
        //rangeOfString 前面的参数是要被搜索的字符串，后面的是要搜索的字符
        //NSNotFound 表示请求操作的某个内容没有发现
        if ([model.cityName rangeOfString:searchText].location != NSNotFound) {
            
            [self.searchArray addObject:model];
            NSLog(@"searchArray = %@",self.searchArray);
        }
        
        //每次请求完数据要reloadData
        [self.detailTV reloadData];
    }
    
}
//点击事件,把地点传回去

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HXLLeavesModel *smallModel = [self.searchArray objectAtIndex:indexPath.row];

    
    [self.detailDelegate modelToPlanViewController:smallModel];
    [self.navigationController popViewControllerAnimated:YES];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.active == YES) {
        return self.searchArray.count;

    }else {
        return 0;
    }
    
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40*SCALEH;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifer = @"searchCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
    if (cell==nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifer];
        
    }
    
    if (_active == YES) {
        
    
    HXLLeavesModel *model = [self.searchArray objectAtIndex:indexPath.row];

    //        高亮搜索
    NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:model.cityName];
    [att addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:[model.cityName rangeOfString:[self.searchBar text]]];
    
    cell.textLabel.attributedText = att;
   
    cell.textLabel.text =  model.cityName;

    
    
    return cell;
    }else {
        return nil;
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
